package com.base2.webshop.client.presenters;

import java.util.Date;
import java.util.List;

import com.base2.webshop.client.constants.NamedTokens;
import com.base2.webshop.client.factories.ClientFactory;
import com.base2.webshop.client.uihandlers.MainUiHandlers;
import com.base2.webshop.shared.factory.WebShopRequestFactory;
import com.base2.webshop.shared.proxies.ProductProxy;
import com.base2.webshop.shared.requests.ProductRequest;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.Request;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;
import com.gwtplatform.mvp.client.proxy.RevealRootContentEvent;

public class MainPresenter extends
		Presenter<MainPresenter.IMainView, MainPresenter.IMainProxy> implements MainUiHandlers{

	private ClientFactory clientFactory;
	
	private WebShopRequestFactory requestFactory;
	
	@Inject
	public MainPresenter(EventBus eventBus, IMainView view, IMainProxy proxy) {
		super(eventBus, view, proxy);
		getView().setUiHandlers(this);
	}
	
	@Inject
	public void setClientFactory(ClientFactory clientFactory){
		this.clientFactory=clientFactory;
	}
	
	@Inject
	public void setProductRequest(WebShopRequestFactory requestFactory){
		this.requestFactory=requestFactory;
	}
	public interface IMainView extends View, HasUiHandlers<MainUiHandlers>{
	}

	@ProxyStandard
	@NameToken(NamedTokens.MAIN)
	public interface IMainProxy extends ProxyPlace<MainPresenter> {
	}

	public static final Type<RevealContentHandler<?>> LOGIN_SLOT_TYPE = new Type<RevealContentHandler<?>>();

	public static final Type<RevealContentHandler<?>> SEARCH_SLOT_TYPE = new Type<RevealContentHandler<?>>();

	public static final Type<RevealContentHandler<?>> PRODUCT_DETAIL_SLOT_TYPE = new Type<RevealContentHandler<?>>();

	@Override
	protected void revealInParent() {
		RevealRootContentEvent.fire(this, this);
	}

	@Override
	public void onReveal() {
		setInSlot(LOGIN_SLOT_TYPE,clientFactory.getLoginPresenter());
		setInSlot(SEARCH_SLOT_TYPE,clientFactory.getSearchMiniPresenter());
		setInSlot(PRODUCT_DETAIL_SLOT_TYPE,clientFactory.getProductDetailPresenter());
	}

	//Just for practice. Don't forget to remove
	@Override
	public void doSomething() {
		Request<ProductProxy> productlistingReq=requestFactory.getProductRequest().findProduct(new Long(1));
		productlistingReq.fire(new Receiver<ProductProxy>() {
			@Override
			public void onSuccess(ProductProxy product) {
				Window.alert(product.getProductName());
			}
		});
		
		
		/*ProductProxy product=requestFactory.getProductRequest().create(ProductProxy.class);
		product.setProductAddedDate((new Date()).toString());
		product.setProductName("Hello");
		Request<Void> createRequest=requestFactory.getProductRequest().persist().using(product);
		createRequest.fire(new Receiver<Void>() {
			@Override
			public void onSuccess(Void response) {
				Window.alert("HELLO");
			}
			
		});*/
	}
}
